import {query, detail} from '../services/blog'
import {ListView} from 'antd-mobile'

export default{
    
    namespace: "index",

    state:{
        selectedTab: "blogTab",
        title: "博客",
        blog: false,
        essence: false,
        detail: false,
        currentDetail: {},
        detailHtml: '',
        detailLoading: false,
    },

    subscriptions:{
        setup({dispatch, history}){
            history.listen(location =>{
                if(location.pathname == "/"){
                    dispatch({
                        type: 'showTab',
                        payload: 'blog'
                    })
                }
            })
        },
    },

    effects:{
        *loaddetail({payload}, {select, call, put}){
            yield put({type: 'detailLoading'})

            const id = payload.id

            const {data} = yield call(detail, {id})

            console.log(data)

            if(data){
                yield put({
                    type: 'loadSuccess',
                    payload:{
                        html: data.string
                    }
                })
            }
        }
    },

    reducers:{
        changeTab(state, action){

            console.log(action)

            return {...state, ...action.payload}
        },
        showTab(state, action){
            console.log(action.payload)

            state[action.payload] = true

            return {...state}
        },
        openDetail(state, action){
            state.detail = true

            return {...state, detail: true, ...action.payload}
        },
        closeDetail(state){
            state.detail = false

            return {...state, detail: false, detailHtml: ''}
        },
        detailLoading(state){
            return {...state, detailLoading: true}
        },
        loadSuccess(state, action){

            return {...state, detailHtml: action.payload.html, detailLoading: false}
        }
    },
}